package com.yakami.ocean.view.fragment;

import android.app.ProgressDialog;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.jakewharton.rxbinding.view.RxView;
import com.trello.rxlifecycle.FragmentEvent;
import com.yakami.ocean.R;
import com.yakami.ocean.ServerAPI;
import com.yakami.ocean.event.Event;
import com.yakami.ocean.event.RxBus;
import com.yakami.ocean.utils.Tools;
import com.yakami.ocean.view.fragment.base.BaseFragment;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Yakami, Created on 2016/4/27
 */
public class SignUpFragment extends BaseFragment {

    @Bind(R.id.user_name) EditText mUserName;
    @Bind(R.id.password) EditText mPwd;
    @Bind(R.id.password_check) EditText mPwdCheck;
    @Bind(R.id.email) EditText mEmail;
    @Bind(R.id.validate_code) EditText mValidateCode;
    @Bind(R.id.img_validate_code) ImageView mValidateCodeImg;
    @Bind(R.id.btn_sign_up) Button mBtnSignUp;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceBundle) {
        View view = inflater.inflate(R.layout.fragment_sign_up, container, false);
        ButterKnife.bind(this, view);

//        Picasso.with(mContext).load(ServerAPI.getValidateCode()).into(mValidateCodeImg);
        ServerAPI.getOceanAPI()
                .getValidateCode()
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .subscribe(responseBody -> {
                    InputStream input = responseBody.byteStream();
                    mValidateCodeImg.setImageBitmap(BitmapFactory.decodeStream(input));
                }, Throwable::printStackTrace);


        RxView.clicks(mBtnSignUp)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    if (Tools.isEmpty(mUserName.getText().toString()) ||
                            Tools.isEmpty(mPwd.getText().toString()) ||
                            Tools.isEmpty(mPwdCheck.getText().toString()) ||
                            Tools.isEmpty(mEmail.getText().toString()) ||
                            Tools.isEmpty(mValidateCode.getText().toString())) {
                        toast(mRes.getString(R.string.null_content));
                        return;
                    }
                    if (!mPwd.getText().toString().equals(mPwdCheck.getText().toString())) {
                        toast(mRes.getString(R.string.password_unmatched));
                        return;
                    }
                    ProgressDialog dialog = new ProgressDialog(getActivity());
                    dialog.setTitle(mRes.getString(R.string.loading));
                    dialog.setCancelable(false);
                    dialog.show();

                    ServerAPI.getOceanAPI()
                            .signUp(mUserName.getText().toString(),
                                    mPwd.getText().toString(),
                                    mPwdCheck.getText().toString(),
                                    mEmail.getText().toString(),
                                    mValidateCode.getText().toString())
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(strResult -> {
                                switch (strResult.result) {
                                    case "codeError":
                                        toast("验证码错误");
                                        break;
                                    case "unmatchedPwd":
                                        toast("两次密码不匹配");
                                        break;
                                    case "nameExisted":
                                        toast("用户名已存在");
                                        break;
                                    case "cannotBeNull":
                                        toast("用户名、密码、邮箱等不能为空");
                                        break;
                                    case "OK!":
                                        toast("注册成功");
                                        (new LoginFragment()).login(mUserName.getText().toString(), mPwd.getText().toString());
                                        RxBus.with(this)
                                                .setEvent(Event.EventType.LOGIN_SUCCESS)
                                                .setEndEvent(FragmentEvent.DESTROY)
                                                .onNext(event -> {
                                                    getActivity().finish();
                                                }).create();
                                        break;
                                }
                                dialog.cancel();
                            }, throwable -> {
                                throwable.printStackTrace();
                                toast("网络异常");
                                dialog.cancel();
                            });

                });

        return view;
    }


}
